﻿using AyaWo.Commen;
using AyaWo.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Qbqqban
{
    public class QbqqbanAuto : ISpler, Spler
    {
        bool _lock = true;
        int _number = 1;
        public QbqqbanAuto()
        {
            this.DatName = "QbqqbanAuto";
            this.SplerName = "qqban发布";
        }
        public void Initiali()
        {
            try
            {
                if (_lock)
                {
                    _lock = false;
                    var listSp = spcontentService.GetLists().OrderByDescending(x=>x.CreateTime).ToList();
                    if (listSp != null)
                    {
                        var list = listSp.Where(x => x.HasTonbu == null).ToList();
                        foreach (var t in list)
                        {
                            if (_number < 2)
                            {
                                if (BlogEasayBll.CanInserts(t.Title, t.typ))
                                {
                                    if (!CopyFile(t.ID)) return;
                                    ToDoContent(t);
                                    spcontentService.UpdateSet(t.ID);
                                    _number++;
                                }
                            }
                        }
                    }
                    _number = 1;
                }
                _lock = true;
            }
            catch (Exception ex)
            {
                _lock = true;
                Logser.Error(this.SplerName + "Initiali: " + ex.ToString());
            }
        }

        void ToDoContent(spidercontent t)
        {
            try
            {
                ContentEasay ce = new Qbqqban.ContentEasay();
                if (t.typ == 1)
                {
                    if (BlogColumnBll.canAdd(t.CountType) == true)
                    {
                        BlogColumnBll.AddType(t.CountType);
                    }
                    string typeid = BlogColumnBll.GetID(t.CountType);

                    if (typeid.NotNullOrWhiteSpace())
                    {
                        ce.columnid = typeid;
                    }
                }
                ce.title = t.Title;
                ce.man = t.Autos;
                ce.content = t.Content.Replace("www.sanwen.net","qqban.top");
                ce.sourcename = t.Source??"";
                ce.ptypeID = t.typ + "";
                ce.CreatedBy = t.typ + "";
                ce.CreatedDate = ToDate( t.BuildTime);
                ce.Add();
            }
            catch (Exception ex)
            {
                Logser.Error(this.SplerName + "-content: " + ex.Message);
            }
        }
        DateTime ToDate(string data)
        {
            if (data == null)
                return DateTime.Now;
            DateTime result;
            return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.Now;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyID"></param>
        /// <returns></returns>
        public bool CopyFile(string keyID)
        {
            try
            {
                string oldPath = AppDomain.CurrentDomain.BaseDirectory + "\\Images\\" + keyID;
                string newPath = DatHelper<string>.GetDatStr(this.DatName + "_Time.dat");
                if (newPath.IsNullOrWhiteSpace())
                {
                    return false;
                }
                else
                {
                    newPath = newPath + "\\" + keyID;
                    if (!DirFileHelper.IsExistDirectory(oldPath))
                        return true;
                    else
                    {
                        if (!DirFileHelper.IsExistDirectory(newPath))
                            DirFileHelper.CreateDir(newPath);
                        DirFileHelper.CopyFolder(oldPath, newPath);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Logser.Error(this.SplerName+ "-CopyFile: "+ex.Message);
                return false;
            }
        }
    }
}
